package com.jcf.springboot.common.utils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class DownloadUtils {
    public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response,String returnName) throws IOException{
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        returnName = response.encodeURL(new String(returnName.getBytes(),"ISO-8859-1"));			//保存的文件名,必须和页面编码一致,否则乱码
        response.addHeader("content-disposition", "attachment;filename=" + returnName);
        response.setContentLength(byteArrayOutputStream.size());
        ServletOutputStream outputStream = response.getOutputStream();
        byteArrayOutputStream.writeTo(outputStream);
        byteArrayOutputStream.close();
        outputStream.flush();
    }
}
